package com.cskaoyan.controller;

import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.MarketTopicInfo;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.bo.AdListParamBo;
import com.cskaoyan.module.pojo.MarketAd;
import com.cskaoyan.module.pojo.MarketTopic;
import com.cskaoyan.module.vo.MarketAdVo;
import com.cskaoyan.module.vo.MarketTopicBo;
import com.cskaoyan.module.vo.MarketTopicVo;
import com.cskaoyan.service.AdService;
import com.cskaoyan.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * AdController
 *
 * @param
 * @return
 * @2022/4/7
 **/
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    public BaseRespVo<MarketTopicVo> list(BaseParam baseParam,String title,String subtitle) {
        MarketTopicBo params=new MarketTopicBo();
        params.setLimit(baseParam.getLimit());
        params.setOrder(baseParam.getOrder());
        params.setPage(baseParam.getPage());
        params.setSort(baseParam.getSort());
//            优惠卷不限制 ->根据limit=0查询

        if (title != null) {
            params.setTitle(title);
        } else {
            params.setTitle(null);
        }
        if (subtitle != null) {
            params.setSubtitle(subtitle);
        } else {
            params.setSubtitle(null);
        }
        MarketTopicVo marketTopic=new MarketTopicVo();
        marketTopic=topicService.list(params);
        System.out.println("topic list marketTopic = " + marketTopic);

        if (marketTopic != null) {
            return BaseRespVo.ok(marketTopic);
        } else {
            return BaseRespVo.invalidData("显示商品出错！");
        }
    }
    @RequestMapping("create")
    public BaseRespVo<MarketTopic> create(@RequestBody MarketTopic params) {

        MarketTopic marketTopic=topicService.create(params);
        if (marketTopic != null) {
            return BaseRespVo.ok(marketTopic);
        } else {
            return BaseRespVo.invalidData("创建出错！");
        }
    }
    @RequestMapping("update")
    public BaseRespVo<MarketTopic> update(@RequestBody MarketTopic params) {

        MarketTopic marketTopic=topicService.update(params);
        if (marketTopic != null) {
            return BaseRespVo.ok(marketTopic);
        } else {
            return BaseRespVo.invalidData("更新出错！");
        }
    }
    @RequestMapping("read")
    public BaseRespVo<MarketTopicInfo> read(Integer id) {

        MarketTopicInfo marketTopic=topicService.read(id);
        System.out.println("<MarketTopicInfo> read marketTopic = " + marketTopic);
        if (marketTopic != null) {
            return BaseRespVo.ok(marketTopic);
        } else {
            return BaseRespVo.invalidData("更新出错！");
        }

    }
    @RequestMapping("delete")
    public BaseRespVo<MarketTopic> delete(@RequestBody MarketTopic params) {
        int code = topicService.delete(params);
        if (code == 0) {
            return BaseRespVo.invalidData();
        } else {
            return BaseRespVo.ok("成功");
        }
    }

    @RequestMapping("batch-delete")
    public BaseRespVo<MarketTopic> batchDelete(@RequestBody String ids) {
//      ids = {"ids":[316,315]}
        System.out.println("ids = " + ids);
        int index = ids.indexOf(":");
        int end = ids.indexOf("]");
        String substring = ids.substring(index + 2,end);
        substring.replace("[","").replace("]","");
        System.out.println("substring = " + substring);
        String[] split = substring.split(",");
        List<Integer> param=new ArrayList<>();
        for (String s : split) {
            int i = Integer.parseInt(s);
            param.add(i);
        }
        int code = topicService.batchDelete(param);
        System.out.println("topic  batchDeletecode = " + code);
        if (code == 0) {
            return BaseRespVo.invalidData();
        } else {
            return BaseRespVo.ok("成功");
        }

    }
}
